package com.test.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cs.framework.common.bo.Message;
import com.cs.framework.ui.bo.UIData;
import com.cs.framework.ui.web.UI;
import com.cs.framework.web.annotation.Action;
import com.cs.framework.web.annotation.Mapping;
import com.cs.framework.web.handler.Web;
import com.test.dto.UserDto;
import com.test.service.UserService;

@Action("user")
public class UserAction {

	private Log logger = LogFactory.getLog(getClass());

	@Resource
	private UserService userService;

	@Mapping
	public void mainPage() {
		UI.returnPage("user_page");
	}

	/**
	 * 查询用户
	 */
	@Mapping
	public Object query() {
		UserDto userDto = Web.bind(UserDto.class);

		List<UserDto> list = userService.query(userDto);
		int count = userService.count(userDto);

		return new UIData(list, count);
	}

	/**
	 * 新增用户
	 */
	@Mapping
	public Object save() {
		UserDto userDto = Web.bind(UserDto.class);

		try {
			userService.save(userDto);

			return new Message("Y", "新增成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);

			return new Message("N", e.getMessage());
		}
	}

	/**
	 * 修改用户
	 */
	@Mapping
	public Object update() {
		UserDto userDto = Web.bind(UserDto.class);

		try {
			userService.update(userDto);

			return new Message("Y", "修改成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);

			return new Message("N", e.getMessage());
		}
	}

}
